﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using YT.Commons;
using YT.DB;
using YT.Modules;

namespace YT.XH.NOSE
{
    public partial class frmNSadultQOLMtn : Form
    {
        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrSickRecID = string.Empty;
        public frmNSadultQOLMtn()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FormStatus mfrmStatus = new FormStatus();
            mfrmStatus = FormStatus.AddNew;
            Form frmBefore = null;
            string strSQL = string.Empty;
            strSQL = "select  datediff(Year, getdate() ,birth) from mSickPerson where SickRecID='" + SysVar.SickRecID + "'";
            DataRow drYear = mdbcon.GetDataFirstRow(strSQL);
            if (drYear != null)
            {
                int intAge = Math.Abs(CvtFunc.ToInt(drYear[0]));
                if (intAge < 12)
                    frmBefore = new frmNS6To12QOL(mstrSickRecID);
                else if (intAge >= 12 && intAge <= 17)
                    frmBefore = new frmNS12_17QOL(mstrSickRecID, "");
                else
                    frmBefore = new frmNSadultQOL(mstrSickRecID, "");
            }

            frmBefore.ShowDialog();
            FillDetail();
        }

        private void FillBaseInfo()
        {
            string strSQL = string.Empty;
            strSQL = "select * from mSickPerson where sickRecID='" + mstrSickRecID + "'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                txtSickName.Text = CvtFunc.ToString(drRow["SickName"]);
                txtAddress.Text = CvtFunc.ToString(drRow["Address"]);
                txtPhone.Text = CvtFunc.ToString(drRow["Phone"]) + "  " + CvtFunc.ToString(drRow["MobilePhone"]);
                txtRemark.Text = CvtFunc.ToString(drRow["Remark"]);
            }
        }

        private bool FillDetail()
        {
            this.dgvDetail.Rows.Clear();
            string strSQL = string.Empty;
            int intRow = 0;
            strSQL = "select * from ExamBeforeTreatHdr where sickRecID='" + mstrSickRecID + "' and Type='" + TitleType.NOSE + "'  order by SeqNo";
            DataTable dtDtl = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtDtl.Rows)
            {
                dgvDetail.Rows.Add();
                this.dgvDetail.Rows[intRow].Cells[colRecID.Index].Value = CvtFunc.ToString(drRow["RecID"]);
                this.dgvDetail.Rows[intRow].Cells[colDate.Index].Value = BaseFunctions.FormatDate(CvtFunc.ToString(drRow["ExamDate"]), DateFormatOption.DateFmtYMD);
                this.dgvDetail.Rows[intRow].Cells[ColSeqNo.Index].Value = CvtFunc.ToString(drRow["SeqNo"]);
                this.dgvDetail.Rows[intRow].Cells[ColRemark.Index].Value = CvtFunc.ToString(drRow["Remark"]);
                intRow++;
            }
            return true;
        }

        private void frmNSadultQOLMtn_Load(object sender, EventArgs e)
        {
            this.Text = "生活质量表";
            FillBaseInfo();
            FillDetail();
            timer1.Start();
        }

        private void dgvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colDetail.Index)
            {
                ShowMtn();
            }      
        }

        private void ShowMtn()
        {
            string strSeqNo = string.Empty;
            strSeqNo = CvtFunc.ToString(this.dgvDetail.CurrentRow.Cells[ColSeqNo.Index].Value);
            DateTime datCheck = CvtFunc.ToDateTime( this.dgvDetail.CurrentRow.Cells[colDate .Index].Value);    
            Form frmBefore = null;
            //这里写要填写的具体内容,就是“治疗前的MTN"
            FormStatus mfrmStatus = new FormStatus();
            mfrmStatus = FormStatus.Update;

            string strSQL = string.Empty;
            string strAgeType = string.Empty;   // =1 12岁以下;  =2 12-16  ;  否则是成年人; 
            strSQL = "select top 1 hdrseqno from ExamBeforTreatResult where SickRecID='" + SysVar.SickRecID + "' and ExamSeqNo='" + strSeqNo + "'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                strAgeType = CvtFunc.ToString(drRow["HdrSeqNo"]); 
            }
        

            strSQL = "select  datediff(Year, getdate() ,birth) from mSickPerson where SickRecID='" + SysVar.SickRecID + "'";
            DataRow drYear = mdbcon.GetDataFirstRow(strSQL);
            if (drYear != null)
            {
                if (strAgeType != string.Empty)
                {
                    if (strAgeType =="1")
                        frmBefore = new frmNS6To12QOL(mstrSickRecID, strSeqNo);
                    else if (strAgeType=="2")
                        frmBefore = new frmNS12_17QOL(mstrSickRecID, strSeqNo);
                    else
                        frmBefore = new frmNSadultQOL(mstrSickRecID, strSeqNo);
                }
                else
                {
                    int intAge = Math.Abs(CvtFunc.ToInt(drYear[0]));
                    if (intAge < 12)
                        frmBefore = new frmNS6To12QOL(mstrSickRecID, strSeqNo);
                    else if (intAge >= 12 && intAge <= 17)
                        frmBefore = new frmNS12_17QOL(mstrSickRecID, strSeqNo);
                    else
                        frmBefore = new frmNSadultQOL(mstrSickRecID, strSeqNo);
                }
            }
            frmBefore.ShowDialog();
            FillDetail();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
                timer1.Stop();
            this.Opacity += 0.04;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!UpdateData()) return;
            this.Close();
        }

        private bool UpdateData()
        {
            this.dgvDetail.EndEdit();
            DBAccess dbcon = new DBAccess(false, false);
            foreach (DataGridViewRow dgvRow in this.dgvDetail.Rows)
            {
                string strSQL = string.Empty;
                string strRemark = CvtFunc.ToString(dgvRow.Cells[ColRemark.Index].Value);
                string strRecID = CvtFunc.ToString(dgvRow.Cells[colRecID.Index].Value);
                strSQL = "update ExamBeforeTreatHdr set Remark = '" + strRemark + "' where RecID='" + strRecID + "'";
                dbcon.ExecuteSql(strSQL);
            }
            dbcon.CloseCon();
            return true;
        }

    }
}
